package com.tiger.ejbql.sample2;

import java.util.*;
import javax.ejb.*;
import javax.naming.*;

public class EJBQLTest2Bean implements javax.ejb.SessionBean {
	private javax.ejb.SessionContext sessionContext = null;

	public void setSessionContext(javax.ejb.SessionContext sessionContext) {
		this.sessionContext = sessionContext;
	}

	public void createTestData() {
		try {
			InitialContext ctx = new InitialContext();
			EJBQLA2Home aHome = (EJBQLA2Home) ctx.lookup("java:comp/env/ejb/EJBQLA2Bean");
			EJBQLB2Home bHome = (EJBQLB2Home) ctx.lookup("java:comp/env/ejb/EJBQLB2Bean");
			EJBQLC2Home cHome = (EJBQLC2Home) ctx.lookup("java:comp/env/ejb/EJBQLC2Bean");

			// テストデータの作成
			EJBQLA2 a1 = aHome.create(new Integer(1), "A1");
			EJBQLA2 a2 = aHome.create(new Integer(2), "A2");

			EJBQLB2 b1 = bHome.create(new Integer(1), "B1");
			EJBQLB2 b2 = bHome.create(new Integer(2), "B2");
			EJBQLB2 b3 = bHome.create(new Integer(3), "B1");
			EJBQLB2 b4 = bHome.create(new Integer(4), "B1");

			EJBQLC2 c1 = cHome.create(new Integer(1), "C1");
			EJBQLC2 c2 = cHome.create(new Integer(2), "C1");
			EJBQLC2 c3 = cHome.create(new Integer(3), "C2");

			a1.getEjbqlb2().add(b1);
			b1.getEjbqlc2().add(c1);
			a1.getEjbqlb2().add(b2);
			b2.getEjbqlc2().add(c2);

			a2.getEjbqlb2().add(b3);
			b3.getEjbqlc2().add(c3);
			a2.getEjbqlb2().add(b4);
		} catch (javax.naming.NamingException e) {
			throw new javax.ejb.EJBException(e);
		} catch (javax.ejb.CreateException e) {
			throw new javax.ejb.EJBException(e);
		}
	}

	public void test() {
		try {
			InitialContext ctx = new InitialContext();
			EJBQLA2Home aHome = (EJBQLA2Home) ctx.lookup("java:comp/env/ejb/EJBQLA2Bean");
			EJBQLB2Home bHome = (EJBQLB2Home) ctx.lookup("java:comp/env/ejb/EJBQLB2Bean");
			EJBQLC2Home cHome = (EJBQLC2Home) ctx.lookup("java:comp/env/ejb/EJBQLC2Bean");

			System.out.println("findByNames1@BHome:");
			Iterator namesB1Results = bHome.findByNames1("A1", "B1").iterator();
			while (namesB1Results.hasNext()) {
				EJBQLB2 b = (EJBQLB2) namesB1Results.next();
				System.out.println("id = " + b.getId() + " name = " + b.getName());
			}
			System.out.println();
			System.out.println("findByNames2@BHome:");
			Iterator namesB2Results = bHome.findByNames2("A1", "B1").iterator();
			while (namesB2Results.hasNext()) {
				EJBQLB2 b = (EJBQLB2) namesB2Results.next();
				System.out.println("id = " + b.getId() + " name = " + b.getName());
			}
			System.out.println();
			System.out.println("findByNames3@BHome:");
			Iterator namesB3Results = bHome.findByNames3("A1", "B1").iterator();
			while (namesB3Results.hasNext()) {
				EJBQLB2 b = (EJBQLB2) namesB3Results.next();
				System.out.println("id = " + b.getId() + " name = " + b.getName());
			}

			System.out.println();
			System.out.println("findByNames1@CHome:");
			Iterator namesC1Results = cHome.findByNames1("A1", "B1", "C1").iterator();
			while (namesC1Results.hasNext()) {
				EJBQLC2 c = (EJBQLC2) namesC1Results.next();
				System.out.println("id = " + c.getId() + " name = " + c.getName());
			}
			System.out.println();
			System.out.println("findByNames2@CHome:");
			Iterator namesC2Results = cHome.findByNames2("A1", "B1", "C1").iterator();
			while (namesC2Results.hasNext()) {
				EJBQLC2 c = (EJBQLC2) namesC2Results.next();
				System.out.println("id = " + c.getId() + " name = " + c.getName());
			}
			System.out.println();
			System.out.println("findByNames3@CHome:");
			Iterator namesC3Results = cHome.findByNames3("A1", "B1", "C1").iterator();
			while (namesC3Results.hasNext()) {
				EJBQLC2 c = (EJBQLC2) namesC3Results.next();
				System.out.println("id = " + c.getId() + " name = " + c.getName());
			}
		} catch (javax.naming.NamingException e) {
			throw new javax.ejb.EJBException(e);
		} catch (javax.ejb.FinderException e) {
			throw new javax.ejb.EJBException(e);
		}
	}

	public void ejbCreate() {
	}

	public void ejbActivate() {
	}

	public void ejbPassivate() {
	}

	public void ejbRemove() {
	}
}
